﻿namespace XGameLibrary.Input
{
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;

    public class InputHandler : GameComponent
    {
        public InputHandler(Game game) :
            base(game)
        {
            CurrentKeyboardState = Keyboard.GetState();
        }

        public static KeyboardState CurrentKeyboardState { get; private set; }

        public static KeyboardState LastKeyboardState { get; private set; }

        public static bool KeyReleased(Keys key)
        {
            return CurrentKeyboardState.IsKeyUp(key) && LastKeyboardState.IsKeyDown(key);
        }

        public static bool KeyPressed(Keys key)
        {
            return CurrentKeyboardState.IsKeyDown(key) && LastKeyboardState.IsKeyUp(key);
        }

        public static bool KeyDown(Keys key)
        {
            return CurrentKeyboardState.IsKeyDown(key);
        }

        public static void Flush()
        {
            LastKeyboardState = CurrentKeyboardState;
        }

        public override void Update(GameTime gameTime)
        {
            LastKeyboardState = CurrentKeyboardState;
            CurrentKeyboardState = Keyboard.GetState();

            base.Update(gameTime);
        }
    }
}
